<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let str = '我爱我家'

      str = str.replace(/我/g, '<span style="color: red">我</span>')
      document.body.innerHTML = str

      // 把c找到，替换成大写的
      let str3 = 'abcdgdc'
      str3 = str3.replace(/c/g, 'C')
      document.body.innerHTML = str3

      // 封装高亮的函数
      function highLight(str, keyword) {
        const reg = new RegExp(keyword, 'gi')
        return str.replace(reg, function (item) {
          return `<span style="color: red">${item}</span>`
        })
      }
      console.log(highLight('abxhdAi', 'a'))
    </script>
  </body>
</html>
